package com.lemon.smartsafety.ui.adapter.safe.inspection;

import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.DataInfo;
import com.lemon.smartsafety.entity.safe.inspection.CompanyBuildingItemConclusionVo;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.utils.common.DrawableUtils;
import com.lemon.smartsafety.utils.common.StringUtils;

import java.util.List;

import cn.bingoogolapple.badgeview.BGABadgeTextView;
import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Cooliean on 16/4/4.
 */
public class CheckItemFlowConclusionAdapter extends LemonRcvAdapter<CompanyBuildingItemConclusionVo> {
    private RecyclerTClickListener<CompanyBuildingItemConclusionVo> mListener;

    private int oldPosition = -1;

    public CheckItemFlowConclusionAdapter(@Nullable List<CompanyBuildingItemConclusionVo> data, RecyclerTClickListener listener) {
        super(data);
        this.mListener = listener;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new CheckItemFlowConclusionItem();
    }

    public CompanyBuildingItemConclusionVo getSelected() {
        CompanyBuildingItemConclusionVo vo = null;
        for (int i = 0; i < mDataList.size(); i++) {
            CompanyBuildingItemConclusionVo vo1 = mDataList.get(i);
            if (vo1.isChecked()) {
                vo = vo1;
                break;
            }
        }
        return vo;
    }


    public class CheckItemFlowConclusionItem implements AdapterItem<CompanyBuildingItemConclusionVo> {


        LinearLayout       ripple;
        RelativeLayout   itemMainLayout;
        ImageView        imageIcon;
        BGABadgeTextView itemText;


        private CompanyBuildingItemConclusionVo mData;
        private int                             mPosition;

        @Override
        public int getLayoutResId() {
            return R.layout.item_conclusion;
        }

        @Override
        public void bindViews(View root) {
            ripple = (LinearLayout) root.findViewById(R.id.ripple);
            itemMainLayout = (RelativeLayout) root.findViewById(R.id.itemMainLayout);
            imageIcon = (ImageView) root.findViewById(R.id.imageIcon);
            itemText = (BGABadgeTextView) root.findViewById(R.id.itemText);
        }

        @Override
        public void setViews() {

            ripple.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mPosition == oldPosition) {
                        return;
                    }
                    oldPosition = mPosition;
                    setChecked();
                    if (mListener != null) {
                        mListener.onItemClick(mData, mPosition);
                    }
                }
            });

          /*  ripple.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
                @Override
                public void onComplete(RippleView rippleView) {
                    if (mPosition == oldPosition) {
                        return;
                    }
                    oldPosition = mPosition;
                    setChecked();
                    if (mListener != null) {
                        mListener.onItemClick(mData, mPosition);
                    }
                }
            });*/

        }

        private void setChecked() {
            for (int i = 0; i < mDataList.size(); i++) {
                CompanyBuildingItemConclusionVo vo = mDataList.get(i);
                vo.setChecked(false);
                if (i == mPosition) {
                    vo.setChecked(true);
                }
            }
            notifyDataSetChanged();
        }

        @Override
        public void handleData(CompanyBuildingItemConclusionVo data, int position) {
            mData = data;
            mPosition = position;
            itemText.setText(StringUtils.nullValue(data.getConclusionName()));
            Drawable src = imageIcon.getContext().getResources().getDrawable(R.mipmap.ic_check_circle);
            if (!data.isChecked()) {
                src = imageIcon.getContext().getResources().getDrawable(R.mipmap.ic_radio_button_normal);
            }
            if (DataInfo.RED_MARK.equals(data.getMarkCode())) {
                imageIcon.setImageDrawable(DrawableUtils.$tint(src, imageIcon.getContext().getResources().getColor(R.color.red)));
            } else if (DataInfo.YELLOW_MARK.equals(data.getMarkCode())) {
                imageIcon.setImageDrawable(DrawableUtils.$tint(src, imageIcon.getContext().getResources().getColor(R.color.yellow)));
            } else if (DataInfo.GREEN_MARK.equals(data.getMarkCode())) {
                imageIcon.setImageDrawable(DrawableUtils.$tint(src, imageIcon.getContext().getResources().getColor(R.color.green)));
            }
        }
    }
}